<template>
  <div class="addDepartment">
    <el-form ref="ruleForm" :rules="rules" :model="form" label-width="120px">
      <el-form-item label="部门编号" prop="deptCode">
        <el-input v-model="form.deptCode" />
      </el-form-item>
      <el-form-item label="部门名称" prop="deptName">
        <el-input v-model="form.deptName" />
      </el-form-item>
      <el-form-item label="上级部门ID" prop="superiorDeptId">
        <el-input v-model="form.superiorDeptId" />
      </el-form-item>
      <el-form-item label="备注" prop="reMark">
        <el-input v-model="form.reMark" />
      </el-form-item>
      <!-- <el-form-item label="地区名称" prop="areaName">
        <el-input v-model="form.areaName" />
      </el-form-item> -->
      <el-form-item label="地区名称" prop="areaName">
        <v-distpicker  @province="onChangeProvince" @city="onChangeCity" @area="onChangeArea"></v-distpicker>
      </el-form-item>
    </el-form>
    <div style="text-align: center;">
      <el-button type="primary" @click="onSubmit('ruleForm')">添加</el-button>
      <el-button @click="onCancel">关闭</el-button>
    </div>

  </div>
</template>

<script>
import request from '@/utils/request'
import axios from 'axios'
import qs from 'qs';
import VDistpicker from 'v-distpicker'


export default {
  components: { VDistpicker },
	inject:['reload'],
  data() {
    return {
      institutionList: [],
      roleList: [],
      form: {},
      buttonLoading: false,
      rules: {
        UserName: [
          { required: true, message: '请输入姓名', trigger: 'blur' },
          { min: 2, max: 10, message: '长度在 2 到 10 个字符', trigger: 'blur' }
        ],
        RoleId: [
          { required: true, message: '请输入账号', trigger: 'blur' },
          { min: 1, max: 15, message: '长度在 2 到 15 个字符', trigger: 'blur' }
        ],
        RoleName: [
          { required: true, message: '请选择角色', trigger: 'change' }
        ],
        // institutionId: [
        //   { required: true, message: '请选择单位', trigger: 'change' }
        // ]
      }
    }
  },
  created() {
    // this.getRoleList();
    // this.getInstitutionList();
  },
  mounted() {

  },
  methods: {
    // getRoleList() {
    //   request({
    //     url: '/api/role/getRoleList',
    //     method: 'get',
    //     params: {}
    //   }).then((res) => {
    //     this.listLoading = true
    //     this.roleList = res.result;
    //     this.listLoading = false
    //   }).catch((err) => {
    //     this.$message(err)
    //   })
    // },
    getInstitutionList() {
      // request({
      //   url: '/api/institution/getInstitutionList',
      //   method: 'get',
      //   params: {}
      // }).then((res) => {
      //   this.listLoading = true
      //   this.institutionList = res.result;
      //   this.listLoading = false
      // }).catch((err) => {
      //   this.$message(err)
      // })
    },
    onSubmit(ruleForm) {
      this.$refs[ruleForm].validate((valid) => {
        if (valid) {
          var userId = sessionStorage.getItem("userId");
          var userName = sessionStorage.getItem("userName");
          var token = sessionStorage.getItem("token");
          this.buttonLoading = true;
          this.form.token = token;
          this.form.userId = userId;
          this.form.uName = userName;
          this.form.areaId = '365';
          axios({
          	method:'post',
          	url:'/sysDept/addSysDept',
          	data: qs.stringify(this.form),
          }).then((res) => {
          	if(res.status == 200){
          		this.$emit('closeDialog', false);
          		this.$message(res.data.data);
          		this.$emit('refresh');
          	} else{
          		this.$message(res.data.data)
          	}	
          }).catch((err) => {
            this.buttonLoading = false;
            this.$message(err)
          })
          // request({
          //   url: '/api/sysDept/addSysDept',
          //   method: 'post',
          //   data: this.form
          // }).then((res) => {
          // 	console.log(res)
          //   // this.buttonLoading = false;
          //   // this.$message(res.resultCodeDesc);
          //   // if (res.resultCode === '200') {
          //   //   this.$emit('refresh');
          //   //   this.$emit('closeDialog', false);
          //   // }
          // }).catch((err) => {
          //   this.buttonLoading = false;
          //   this.$message(err)
          // })
        } else {
          this.$message({
            message: '表单验证失败',
            type: 'warning'
          })

          return false;
        }
      });
    },
    //获取省市区信息赋值给form
    onChangeProvince(a){
      this.form.areaName = a.value;
      this.form.areaId = a.code;
    },
    onChangeCity(a){
      this.form.areaName = this.form.areaName + a.value;
      this.form.areaId = a.code;
    },
    onChangeArea(a){
      this.form.areaName = this.form.areaName + a.value;
      this.form.areaId = a.code;
    },
    ////////////////
    onCancel() {
      this.$emit('closeDialog', false);
    }
  }
}
</script>

<style scoped>
.line{
  text-align: center;
}
</style>
